import Mock from 'mockjs'
// import { param2Obj } from '../utils/myUtils'
let List = []
for (var i = 0; i < 100; i++) {
  List.push({
    id: Mock.mock('@natural'),
    name: Mock.mock('@word'),
    phone: Mock.mock(138 + '@increment(100)' + Mock.mock('@increment(1000)')),
    classname: "幼儿园英语入门课程",
    money: '2399.00',
    state: Mock.mock('@integer(0, 4)'),
    time: Mock.mock('@datetime("yyyy-MM-dd  HH:mm")')
  })
}
export default {
  odMockData: (config) => {
    let { search, state, page, pageSize } = JSON.parse(config.body)

    console.log('pageSize', pageSize);
    let newList = []
    if (state >= 0) {
      newList = List.filter(item => {
        return item.state == state
      })
    } else {
      newList = List.filter(item => {
        return item.name.indexOf(search) != -1
      })
      if (newList == '') {
        newList = List.filter(item => {
          return item.phone == search
        })
      }
      if (newList == '') {
        newList = List.filter(item => {
          return item.id == search
        })
      }
      if (newList == '') {
        newList = List.filter(item => {
          return item.time == search
        })
      }
      if (newList == 'state') {
        newList = List.filter(item => {

          return item.state == search
        })
      }
    };
    //分页
    // (p-1)*s <= i < p*s
    let newList2 = newList.filter((item, index) => {
      return (page - 1) * pageSize <= index && index < page * pageSize
    })
    if (newList2.length == 0) {
      page--;
      newList2 = newList.filter((item, index) => {
        return (page - 1) * pageSize <= index && index < page * pageSize
      })
    }

    return {
      code: 200,
      msg: '成功',
      data: newList2,
      total: newList.length,
      page: page,
    }
  },
  //取消
  delMockData: (config) => {
    let { id } = JSON.parse(config.body)
    let newList = List.filter(item => {
      return item.id != id
    })
    List = newList
    return {
      code: 200,
      msg: '取消订单',
      data: null
    }
  },
  //查看
  lookMockData: (config) => {
    let id = config.body
    let a = {};
    List.forEach((r, i) => {
      if (r.id == id) {
        a = r
      }
    }
    )
    return {
      code: 200,
      msg: '查到了',
      data: a,
    }
  },
  //批量删除
  plDeleteData: (config) => {
    let ids = config.body;
    console.log(ids);
    let newList = List.filter(item => {
      return ids.indexOf(item.id) == -1
    })
    List = newList
    return {
      code: 200,
      msg: '批量删除成功',
      data: null
    }
  }
}
